/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Conversation;
import hibernate.entity.Login;
import hibernate.entity.MesageConversation;
import hibernate.entity.MessageType;
import java.util.List;
import hibernate.entity.Message;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.mapping.Collection;
import util.HibernateUtil;

/**
 *
 * @author NDH
 */
public class MessageDAO extends AbstractDAO {

    List< Message> listM;

    public  Message readByID(int id) {
        Query q = HibernateUtil.getSessionFactory().openSession().createQuery("from  Message where MessageID =:id");
        q.setParameter("id", id);
        return ( Message) q.list().get(0);
    }

    public  Message readByConversation(int id) {
        Query q = HibernateUtil.getSessionFactory().openSession().createQuery("from  Message where MessageID =:id");
        q.setParameter("id", id);
        return ( Message) q.list().get(0);
    }

    public List< Message> readByDate(Date date) {
        Query q = HibernateUtil.getSessionFactory().openSession().createQuery("from  Message where dateTime =:date");
        q.setParameter("date", date);
        return q.list();
    }

    public List< Message> readBetweenDates(Date sDate, Date eDate) {
        Query q = HibernateUtil.getSessionFactory().openSession().createQuery("from  Message where dateTime between :d1 and :d2");
        q.setParameter("d1", sDate);
        q.setParameter("d2", eDate);
        return q.list();
    }

    public List< Message> readByFromID(int ID) {
        Query q = HibernateUtil.getSessionFactory().openSession().createQuery("from  Message where fromId =:id");
        q.setParameter("id", ID);
        return q.list();
    }

    public int delete(int id) {
        tx = null;
        try {
            tx = session.beginTransaction();
            Query q = session.createQuery("delete  Message m where m.messageId = :id");
            q.setParameter("id", id);
            int r = q.executeUpdate();
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }

    }

    public int create( Message mes) {
        try {
            Transaction t = session.beginTransaction();
            session.save(mes);
            t.commit();
            return 1;
        } catch (Exception ex) {
            System.out.println("");
            return -1;
        }
    }

    public int create( Message mes, Integer id) {
        tx = session.beginTransaction();
        try {

            session.save(mes);
//            session.refresh(t);
            tx.commit();
            return 1;
        } catch (Exception ex) {
            tx.rollback();
            System.out.println("");
            return -1;
        }
    }

    public List< Message> readByReceiverID(int ID) {
        session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery("from  Message as m  join fetch m.messageConversations as mr"
                + " where mr.receiver.accId = :id");
        //join mc.conversations as c distinc(c)
        q.setParameter("id", ID);
        return q.list();
    }

    public List< Message> readByConversation(Conversation con) {
        listM = new ArrayList< Message>();
        List<MesageConversation> listMC = new ArrayList<MesageConversation>();
        Iterator<MesageConversation> iMC = con.getMesageConversations().iterator();
        while (iMC.hasNext()) {
            MesageConversation mc = iMC.next();
            listM.add(mc.getMessage());
        }
        return listM;
    }

//    public List< Message> readByConversation( Message m, Logins logins) {
//        Conversations con = null;
//        listM = new ArrayList< Message>();
//        Iterator<MessageConversation> iMC = m.getMessageConversations().iterator();
//        if (iMC.hasNext()) {
//            MessageConversation mc = iMC.next();
//            if (mc.ge) {
//                
//            }
//        }
//        return listM;
//    }
    public List< Message> readAssignment(Login from) {
        try {
//            tx = session.beginTransaction();
            query = session.createQuery("select m from  Message as  m  join  m.messageConversations as mc join  m.messageTypes as mt "
                    + "where mt.typeName = 'Assignment' "
                    + "and mc.receiver =:login");
            query.setParameter("login", from);
            return query.list();

        } catch (Exception e) {
//            tx.commit();
            return null;
        }


    }

//    public List< Message> readAssignment(int recID) {
//        query = session.createQuery("select new  Message(m.from, m.messageTypes, m.content, m.dateTime, m.title, "
//                + "m.messageReceivers, m.messageConversations, "
//                + "m.attachmentses) "
//                + "from  Message as  m  join m.messageReceivers as mr join m.messageTypes as mt "
//                + "where mt.typeName = 'Assignment' "
//                + "and mr.accounts.accId =:id");
////        query = session.createSQLQuery("select * from  Message").addScalar("messageID", Hibernate.INTEGER)
//
//        query.setParameter("id", recID);
//        return query.list();
//    }
    public int update( Message mes) {
        try {
            tx = session.beginTransaction();
            session.update(mes);
            tx.commit();
            return 1;

        } catch (Exception ex) {
            return -1;
        }


    }

 

    public List< Message> readByContent(String keyword) {
        String entityName = "";
        query = session.createQuery("from  Message where content like '%"+keyword+"%'");
        return query.list();
    }
}
